package com.czy.owner.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.czy.owner.R;
import com.czy.owner.entity.EnpandableStorePackageEntity;
import com.czy.owner.entity.PackageCardProjectModel;
import com.czy.owner.entity.StorePackageEntity;
import com.czy.owner.ui.packagecardproject.PackageCardDetailActivity;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.widget.shopbutton.AnimShopButton;
import com.czy.owner.widget.shopbutton.IOnAddDelListener;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.List;

/**
 * Created by wangyang on 2018/3/26.
 */
public class PackageExpandableAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private static final String TAG = PackageExpandableAdapter.class.getSimpleName();
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    private int themeColor = Color.parseColor("#09c0c7");
    private int blackColor = Color.parseColor("#373737");
    protected OnItemClickListener onItemClickListener;
    protected OnBuyItemClickListener onBuyItemClickListener;
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public PackageExpandableAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_sub_store_package_top);
        addItemType(TYPE_LEVEL_1, R.layout.item_sub_store_package);
    }


    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_0:
                final StorePackageEntity.DataBean lv0 = (StorePackageEntity.DataBean) item;
                holder.setText(R.id.tv_package_name, lv0.getPackageName());
                MyLog.e("yang",lv0.getPackageName());
                holder.setImageResource(R.id.img_store_package, lv0.isExpanded() ? R.mipmap.expand_top_two : R.mipmap.expand_bottom_two);
                holder.getView(R.id.rel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                final EnpandableStorePackageEntity lv1 = (EnpandableStorePackageEntity) item;
                EasyRecyclerView easy_recycleview_sub = (EasyRecyclerView) holder.getView(R.id.easy_recycleview_sub);
                TextView tv_give_amount = (TextView) holder.getView(R.id.tv_give_amount);
                TextView tv_use_time = (TextView) holder.getView(R.id.tv_use_time);
                TextView tv_old_price = (TextView) holder.getView(R.id.tv_old_price);
                TextView tv_new_price = (TextView) holder.getView(R.id.tv_new_price);
                TextView tv_buy = (TextView) holder.getView(R.id.tv_buy);
                AnimShopButton btn_anmi_shop = (AnimShopButton) holder.getView(R.id.btn_anmi_shop);
                LinearLayout ll_give_amount = (LinearLayout) holder.getView(R.id.ll_give_amount);



                btn_anmi_shop.setMaxCount(lv1.getSingleUsrMaxCount());
                if (lv1.getCurrentCount() != 1) {
                    btn_anmi_shop.setCount(lv1.getCurrentCount());
                } else {
                    btn_anmi_shop.setCount(1);
                }

                btn_anmi_shop.setOnAddDelListener(new IOnAddDelListener() {
                    @Override
                    public void onAddSuccess(int count) {
                        MyLog.e("yang", "onAddSuccess===" + count);
                        lv1.setCurrentCount(count);

                    }

                    @Override
                    public void onAddFailed(int count, FailType failType) {
                        if (failType == FailType.COUNT_MAX)
                            PhoneUtils.ShowToastMessage(mContext, "不能超出最大购买数量");

                    }

                    @Override
                    public void onDelSuccess(int count) {
                        MyLog.e("yang", "onDelSuccess===" + count);
                        lv1.setCurrentCount(count);

                    }

                    @Override
                    public void onDelFaild(int count, FailType failType) {

                    }
                });

                int validityType = lv1.getValidityType();//0>>>永久有效;1>>>固定时长;2>>>固定时间
                int fixTimeType = lv1.getFixTimeType();//0>>>>>月;1>>>>年
                long startTime = lv1.getStartTime();
                long endTime = lv1.getEndTime();

                if (validityType == 0) {
                    tv_use_time.setText("有效期: 永久有效");
                } else if (validityType == 1) {
                    if (fixTimeType == 0) {
                        tv_use_time.setText("有效期: "+lv1.getFixTimeValue() + "个月有效");
                    } else if (fixTimeType == 1) {
                        tv_use_time.setText("有效期: "+lv1.getFixTimeValue() + "年有效");
                    }
                } else if (validityType == 2) {
                    tv_use_time.setText("有效期: "+ TimeUtils.formatTimeDifference4(startTime) + "-" + TimeUtils.formatTimeDifference4(endTime) + "有效");
                }
                tv_old_price.setText("原价￥" + String.format("%.2f", lv1.getOriginalPrice()));
                tv_new_price.setText(String.format("%.2f", lv1.getmPrice()));
                tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
                if (lv1.getGiveAmount() > 0) {
                    ll_give_amount.setVisibility(View.VISIBLE);
                    tv_give_amount.setText("¥" + DecimalUtil.formatNum(lv1.getGiveAmount() + ""));
                } else {
                    ll_give_amount.setVisibility(View.GONE);

                }

                easy_recycleview_sub.setLayoutManager(new LinearLayoutManager(mContext));
                MyPackageSubAdapter myPackageSubAdapter = new MyPackageSubAdapter(mContext);
                easy_recycleview_sub.setAdapter(myPackageSubAdapter);
                myPackageSubAdapter.addAll(lv1.getItemList());
                myPackageSubAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<StorePackageEntity.DataBean.ItemListBean>() {
                    @Override
                    public void onItemClick(View view, int position, StorePackageEntity.DataBean.ItemListBean o) {
                        Intent ti = new Intent(mContext, PackageCardDetailActivity.class);
                        ti.putExtra("servicePackageId", o.getServicePackageId());
                        mContext.startActivity(ti);
                    }
                });


                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick(lv1.getServicePackageId(), v);
                        }


                    }
                });


                tv_buy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onBuyItemClickListener != null) {
                            onBuyItemClickListener.onBuyItemClick(lv1, v);
                        }
                    }
                });
                break;

        }
    }







    class MyPackageSubAdapter extends RecyclerArrayAdapter<StorePackageEntity.DataBean.ItemListBean> {
        public MyPackageSubAdapter(Context context) {
            super(context);

        }

        @Override
        public com.easyrecycleview.adapter.BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyPackageSubAdapter.MyPackageSubViewHolder(parent);
        }

        class MyPackageSubViewHolder extends com.easyrecycleview.adapter.BaseViewHolder<StorePackageEntity.DataBean.ItemListBean> {
            private TextView tv_name;
            private TextView tv_sum_number;
            private TextView tv_used_number;

            public MyPackageSubViewHolder(ViewGroup parent) {
                super(parent, R.layout.item_my_package_sub);
                tv_name = $(R.id.tv_name);
                tv_sum_number = $(R.id.tv_sum_number);
                tv_used_number = $(R.id.tv_used_number);

            }

            @Override
            public void setData(final StorePackageEntity.DataBean.ItemListBean model) {
                tv_name.setText(model.getItemName());
                tv_sum_number.setGravity(Gravity.RIGHT);
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tv_sum_number.getLayoutParams();
                lp.setMargins(0, 0, PhoneUtils.dip2px(tv_sum_number.getContext(), 30), 0);
                tv_sum_number.setLayoutParams(lp);
                if (!model.isUnlimitedCount()) {
                    tv_sum_number.setText("不限次");
                } else {
                    tv_sum_number.setText("x" + model.getCount());
                }
                tv_sum_number.setTextColor(blackColor);
                tv_used_number.setVisibility(View.GONE);
            }
        }
    }
    public interface OnItemClickListener {
        void onItemClick(int servicePackageId, View view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }



    public interface OnBuyItemClickListener {
        void onBuyItemClick(EnpandableStorePackageEntity enpandableStorePackageEntity, View view);
    }

    public void setOnBuyItemClickListener(OnBuyItemClickListener listener) {
        this.onBuyItemClickListener = listener;
    }

}
